package com.xumt.hwHMSdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.JsonToken;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.xumt.hwHMSdemo.cannectSb.HttpRequest;

import org.json.JSONArray;
import org.json.JSONObject;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class CommodityActivity extends AppCompatActivity {

    private String name;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_commodity);
        findname();
        try {

            JSONArray jsonArray= new JSONArray(name);
            String a[]= new String[jsonArray.length()];
            for (int i=0;i<jsonArray.length();i++)
            {
                JSONObject jsonObject=jsonArray.getJSONObject(i);
                a[i]=((String)jsonObject.get("name"));

            }
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                    CommodityActivity.this, android.R.layout.simple_list_item_1,a);
            ListView listView = (ListView) findViewById(R.id.list_view);
            listView.setAdapter(adapter);

        }catch (Exception e)
        {
            e.printStackTrace();
        }
        //listview
        ListView listView = (ListView) findViewById(R.id.list_view);
        AdapterView.OnItemClickListener itemClickListener=new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent intent=new Intent(CommodityActivity.this,Commodity_message.class);
                intent.putExtra(Commodity_message.EXTRA_ID,position+1);//向第三级Activity传递信息,第一个参数是DrinkActivity定义的key，利用这个key可以提去第二个参数的值。
                startActivity(intent);
            }
        };
        listView.setOnItemClickListener(itemClickListener);


    }



    public void findname()
    {

        HttpRequest request = new HttpRequest();
        new Thread() {
            public void run() {
                try {
                    name=request.sendGet("http://169.254.132.31:8081/Commdity/getall","");

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}